package yemian.demo.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yemian.demo.model.User;
import yemian.demo.service.UserService;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    //登录页面
    @GetMapping("login")//此注解是get请求，不写的话，请求页面时无法出现
    public ModelAndView zhuce() {
        ModelAndView mav = new ModelAndView("/login");//这个是固定格式，括号里可以修改
        mav.addObject("name", "幻神");
        return mav;
    }
    //页面跳转
    @GetMapping("index")
     public ModelAndView index(){
        ModelAndView  mav = new ModelAndView("/index");
        return mav;
     }
    @GetMapping("list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView("/list");
        List<User> byAll = userService.findByAll();
        modelAndView.addObject("users",byAll);
        return modelAndView;
    }


    @PostMapping("login")
    public @ResponseBody
    String login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String pasword){
        User byUsername = userService.findByUsername(username);
        if (byUsername != null){
            if (byUsername.getPassword().equals(pasword)){
                return "1";
            }else {
                return "2";
            }
        }else {
            return "3";
        }

}









    @GetMapping("delete")
    @ResponseBody
    public String delete(@RequestParam(name = "id") Integer id){
        String delete = userService.delete(id);
        return delete;
    }
}
